package toolkit;
import java.io.*;

/**
 * <ul>
 * <li>Title: SerializationUtils</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author 程序员ken
 * @date 2021/4/30 0030 上午 10:05
 */
public class SerializationUtils {

    public SerializationUtils() {
    }

    /**
     *使用序列化对{@code对象}进行深度克隆。
     *<p>这比手工编写克隆方法慢很多倍
     *在对象图形中的所有对象上。但是，对于复杂对象
     *图，或者对于那些不支持深度克隆的人，这可以
     *做一个简单的替代实现。当然所有的东西
     *必须是{@code Serializable}</p>
     *@param<T>所涉及对象的类型
     *@param object 要克隆的{@code Serializable}对象
     *@返回克隆对象
     */
    public static byte[] serialize(Object object) {
        if (object == null) {
            return null;
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);

            try {
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                Throwable var3 = null;

                try {
                    oos.writeObject(object);
                    oos.flush();
                } catch (Throwable var13) {
                    var3 = var13;
                    throw var13;
                } finally {
                    if (oos != null) {
                        if (var3 != null) {
                            try {
                                oos.close();
                            } catch (Throwable var12) {
                                var3.addSuppressed(var12);
                            }
                        } else {
                            oos.close();
                        }
                    }

                }
            } catch (IOException var15) {
                throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), var15);
            }

            return baos.toByteArray();
        }
    }

    /**
     *将给定对象序列化为字节数组。
     *@param bytes 要序列化的对象
     *@返回以可移植方式表示对象的字节数组
     */
    public static Object deserialize(byte[] bytes) {
        if (bytes == null) {
            return null;
        } else {
            try {
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
                Throwable var2 = null;
                Object var3;
                try {
                    var3 = ois.readObject();
                } catch (Throwable var14) {
                    var2 = var14;
                    throw var14;
                } finally {
                    if (ois != null) {
                        if (var2 != null) {
                            try {
                                ois.close();
                            } catch (Throwable var13) {
                                var2.addSuppressed(var13);
                            }
                        } else {
                            ois.close();
                        }
                    }
                }
                return var3;
            } catch (IOException var16) {
                throw new IllegalArgumentException("Failed to deserialize object", var16);
            } catch (ClassNotFoundException var17) {
                throw new IllegalStateException("Failed to deserialize object type", var17);
            }
        }
    }
}

